package com.psp.park.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.northend.util.NumUtil;
import com.psp.park.controller.res.BaseResult;
import com.psp.park.controller.res.ListResult;
import com.psp.park.controller.res.ObjectResult;
import com.psp.park.controller.res.bean.RCooperationBean;
import com.psp.park.controller.res.bean.RUserBean;
import com.psp.park.controller.springmvc.req.CompanyCheckInParam;
import com.psp.park.controller.springmvc.req.DelCooperationParam;
import com.psp.park.controller.springmvc.req.EditCompanyCheckInParam;
import com.psp.park.controller.springmvc.req.GetCooperationListParam;
import com.psp.park.model.ParkUserBean;
import com.psp.park.service.CooperationService;
import com.psp.park.service.UserService;
import com.psp.park.service.exception.ServiceException;
import com.psp.park.service.impl.PageResult;

@Component
public class CooperationController {
	Logger logger = Logger.getLogger(CooperationController.class);

	@Autowired
	CooperationService cooperationServiceImpl;
	
	@Autowired
	UserService userServiceImpl;
	
	
	/**
	 * 获取合作记录列表
	 * @param param
	 * @param request
	 * @param response
	 * @return
	 */
	public ListResult<RCooperationBean> list(GetCooperationListParam param, HttpServletRequest request,
			HttpServletResponse response) {
		ListResult<RCooperationBean> result = new ListResult<>();
		String token = (String) request.getAttribute("token");
		try {
			String puid = (String) request.getAttribute("userId"); // 园区业务员id
			ParkUserBean user = (ParkUserBean)request.getAttribute("user");
			int page = NumUtil.toInt(param.getPage(), 0);
			int pageSize = NumUtil.toInt(param.getPageSize(), 10);
			int cateCode = NumUtil.toInt(param.getCateCode(), 0);
			int handleState = NumUtil.toInt(param.getHandleState(), 0);
			String content = param.getContent();

			PageResult<RCooperationBean> resData = cooperationServiceImpl.getList(page, pageSize, puid, cateCode, content, user, handleState);
			if (resData != null) {
				result.setTotalSize(resData.getCount());
				result.setData(resData.getData());
			}
			result.setPage(page);
		} catch (ServiceException e) {
			result.setServiceException(e);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result.setServiceException(new ServiceException("error", "发送异常"));
		}
		result.setToken(token);
		return result;
	}
	
	/**
	 * 合作登记
	 * @param param
	 * @param request
	 * @param response
	 * @return
	 */
	public ObjectResult<RUserBean> checkIn(CompanyCheckInParam param, HttpServletRequest request, HttpServletResponse response) {
		ObjectResult<RUserBean> result = new ObjectResult<RUserBean>();
		String token = (String) request.getAttribute("token");
		try {
			String puid = (String) request.getAttribute("userId"); // 园区业务员id
			ParkUserBean user = userServiceImpl.getUser(puid);
			RUserBean ru = new RUserBean();
			ru.setAccount(user.getAccount());
			ru.setHeadIcon(user.getHeadIcon());
			ru.setPark(user.getPark());
			ru.setUserName(user.getNickName());
			String name = param.getName();
			String brief = param.getBrief();
			String charterImg = param.getCharterImg();
			String file = param.getFile();
			String userName = param.getUserName();
			String phone = param.getPhone();
			String email = param.getEmail();
			String cateCode = param.getCateCode();
			String cid = null;

			boolean flag = cooperationServiceImpl.EditInfo(puid, cid, name, brief, charterImg, file, userName, phone, email, cateCode);
			result.setFlag(flag);
			result.setData(ru);
		} catch (ServiceException e) {
			result.setServiceException(e);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result.setServiceException(new ServiceException("error", "发生异常"));
		}
		result.setToken(token);
		return result;
	}
	
	/**
	 * 编辑合作记录
	 * @param param
	 * @param request
	 * @param response
	 * @return
	 */
	public BaseResult edit(EditCompanyCheckInParam param, HttpServletRequest request, HttpServletResponse response) {
		BaseResult result = new BaseResult();
		String token = (String) request.getAttribute("token");
		try {
			String puid = (String) request.getAttribute("userId"); // 园区业务员id
			String cid = param.getCid();
			String name = param.getName();
			String brief = param.getBrief();
			String charterImg = param.getCharterImg();
			String file = param.getFile();
			String userName = param.getUserName();
			String phone = param.getPhone();
			String email = param.getEmail();
			String cateCode = param.getCateCode();

			boolean flag = cooperationServiceImpl.EditInfo(puid, cid, name, brief, charterImg, file, userName, phone, email, cateCode);
			result.setFlag(flag);
		} catch (ServiceException e) {
			result.setServiceException(e);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result.setServiceException(new ServiceException("error", "发生异常"));
		}
		result.setToken(token);
		return result;
	}
	
	/**
	 * 获取合作数量
	 * @param request
	 * @param response
	 * @return
	 */
	public ObjectResult<Integer> getNum(HttpServletRequest request, HttpServletResponse response) {
		ObjectResult<Integer> result = new ObjectResult<Integer>();
		String token = (String) request.getAttribute("token");
		try {
			String puid = (String) request.getAttribute("userId"); // 园区业务员id
			int num = cooperationServiceImpl.getNum(puid);
			result.setData(num);
		} catch (ServiceException e) {
			logger.error(e.getMessage(), e);
			result.setServiceException(e);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result.setServiceException(new ServiceException("error", "发送异常"));
		}
		result.setToken(token);
		return result;
	}
	
	/**
	 * 删除合作记录
	 * @param param
	 * @param request
	 * @param response
	 * @return
	 */
	public BaseResult del(DelCooperationParam param, HttpServletRequest request, HttpServletResponse response) {
		BaseResult result = new BaseResult();
		String token = (String) request.getAttribute("token");
		try {
			String puid = (String) request.getAttribute("userId"); // 园区业务员id
			String cid = param.getCid();
			boolean flag = cooperationServiceImpl.deleteById(puid, cid);
			result.setFlag(flag);
		} catch (ServiceException e) {
			result.setServiceException(e);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result.setServiceException(new ServiceException("error", "发生异常"));
		}
		result.setToken(token);
		return result;
	}

}
